//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.lyl.security.utils;


import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Component
public final class RedisUtil {
    private static RedisTemplate<String, Object> REDIS_TEMPLATE;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    public RedisUtil() {
    }

    @PostConstruct
    public void init() {
        REDIS_TEMPLATE = this.redisTemplate;
    }

    public static boolean expire(String key, long time) {
        try {
            if (time > 0L) {
                REDIS_TEMPLATE.expire(key, time, TimeUnit.SECONDS);
            }

            return true;
        } catch (Exception var4) {
            var4.printStackTrace();
            return false;
        }
    }

    public static long getExpire(String key) {
        return REDIS_TEMPLATE.getExpire(key, TimeUnit.SECONDS);
    }

    public static boolean hasKey(String key) {
        try {
            return REDIS_TEMPLATE.hasKey(key);
        } catch (Exception var2) {
            var2.printStackTrace();
            return false;
        }
    }

    public static void del(String... key) {
        if (key != null && key.length > 0) {
            if (key.length == 1) {
                REDIS_TEMPLATE.delete(key[0]);
            } else {
                REDIS_TEMPLATE.delete(Arrays.asList(key));
            }
        }

    }

    public static Object get(String key) {
        return key == null ? null : REDIS_TEMPLATE.opsForValue().get(key);
    }

    public static boolean set(String key, Object value) {
        try {
            REDIS_TEMPLATE.opsForValue().set(key, value);
            return true;
        } catch (Exception var3) {
            var3.printStackTrace();
            return false;
        }
    }

    public static boolean set(String key, Object value, long time) {
        try {
            if (time > 0L) {
                REDIS_TEMPLATE.opsForValue().set(key, value, time, TimeUnit.SECONDS);
            } else {
                set(key, value);
            }

            return true;
        } catch (Exception var5) {
            var5.printStackTrace();
            return false;
        }
    }

    public static boolean set(String key, Object value, long time, TimeUnit unit) {
        try {
            if (time > 0L) {
                REDIS_TEMPLATE.opsForValue().set(key, value, time, unit);
            } else {
                set(key, value);
            }

            return true;
        } catch (Exception var5) {
            var5.printStackTrace();
            return false;
        }
    }

    public static long incr(String key, long delta) {
        if (delta < 0L) {
            throw new RuntimeException("递增因子必须大于0");
        } else {
            return REDIS_TEMPLATE.opsForValue().increment(key, delta);
        }
    }

    public static long decr(String key, long delta) {
        if (delta < 0L) {
            throw new RuntimeException("递减因子必须大于0");
        } else {
            return REDIS_TEMPLATE.opsForValue().decrement(key, delta);
        }
    }

    public static Object hGet(String key, String item) {
        return REDIS_TEMPLATE.opsForHash().get(key, item);
    }

    public static Map<Object, Object> hmGet(String key) {
        return REDIS_TEMPLATE.opsForHash().entries(key);
    }

    public static boolean hmSet(String key, Map<String, Object> map) {
        try {
            REDIS_TEMPLATE.opsForHash().putAll(key, map);
            return true;
        } catch (Exception var3) {
            var3.printStackTrace();
            return false;
        }
    }

    public static boolean hmSet(String key, Map<String, Object> map, long time) {
        try {
            REDIS_TEMPLATE.opsForHash().putAll(key, map);
            if (time > 0L) {
                expire(key, time);
            }

            return true;
        } catch (Exception var5) {
            var5.printStackTrace();
            return false;
        }
    }

    public static boolean hSet(String key, String item, Object value) {
        try {
            REDIS_TEMPLATE.opsForHash().put(key, item, value);
            return true;
        } catch (Exception var4) {
            var4.printStackTrace();
            return false;
        }
    }

    public static boolean hSet(String key, String item, Object value, long time) {
        try {
            REDIS_TEMPLATE.opsForHash().put(key, item, value);
            if (time > 0L) {
                expire(key, time);
            }

            return true;
        } catch (Exception var6) {
            var6.printStackTrace();
            return false;
        }
    }

    public static void hDel(String key, Object... item) {
        REDIS_TEMPLATE.opsForHash().delete(key, item);
    }

    public static boolean hHasKey(String key, String item) {
        return REDIS_TEMPLATE.opsForHash().hasKey(key, item);
    }

    public static double hIncr(String key, String item, double by) {
        return REDIS_TEMPLATE.opsForHash().increment(key, item, by);
    }

    public static double hDecr(String key, String item, double by) {
        return REDIS_TEMPLATE.opsForHash().increment(key, item, -by);
    }

    public static Set<Object> sGet(String key) {
        try {
            return REDIS_TEMPLATE.opsForSet().members(key);
        } catch (Exception var2) {
            var2.printStackTrace();
            return null;
        }
    }

    public static boolean sHasKey(String key, Object value) {
        try {
            return REDIS_TEMPLATE.opsForSet().isMember(key, value);
        } catch (Exception var3) {
            var3.printStackTrace();
            return false;
        }
    }

    public static long sSet(String key, Object... values) {
        try {
            return REDIS_TEMPLATE.opsForSet().add(key, values);
        } catch (Exception var3) {
            var3.printStackTrace();
            return 0L;
        }
    }

    public static long sSetAndTime(String key, long time, Object... values) {
        try {
            Long count = REDIS_TEMPLATE.opsForSet().add(key, values);
            if (time > 0L) {
                expire(key, time);
            }

            return count;
        } catch (Exception var5) {
            var5.printStackTrace();
            return 0L;
        }
    }

    public static long sGetSetSize(String key) {
        try {
            return REDIS_TEMPLATE.opsForSet().size(key);
        } catch (Exception var2) {
            var2.printStackTrace();
            return 0L;
        }
    }

    public static long setRemove(String key, Object... values) {
        try {
            Long count = REDIS_TEMPLATE.opsForSet().remove(key, values);
            return count;
        } catch (Exception var3) {
            var3.printStackTrace();
            return 0L;
        }
    }

    public static List<Object> lGet(String key, long start, long end) {
        try {
            return REDIS_TEMPLATE.opsForList().range(key, start, end);
        } catch (Exception var6) {
            var6.printStackTrace();
            return null;
        }
    }

    public static long lGetListSize(String key) {
        try {
            return REDIS_TEMPLATE.opsForList().size(key);
        } catch (Exception var2) {
            var2.printStackTrace();
            return 0L;
        }
    }

    public static Object lGetIndex(String key, long index) {
        try {
            return REDIS_TEMPLATE.opsForList().index(key, index);
        } catch (Exception var4) {
            var4.printStackTrace();
            return null;
        }
    }

    public static boolean lSet(String key, Object value) {
        try {
            REDIS_TEMPLATE.opsForList().rightPush(key, value);
            return true;
        } catch (Exception var3) {
            var3.printStackTrace();
            return false;
        }
    }

    public static boolean lSet(String key, Object value, long time) {
        try {
            REDIS_TEMPLATE.opsForList().rightPush(key, value);
            if (time > 0L) {
                expire(key, time);
            }

            return true;
        } catch (Exception var5) {
            var5.printStackTrace();
            return false;
        }
    }

    public static boolean lSet(String key, List<Object> value) {
        try {
            REDIS_TEMPLATE.opsForList().rightPushAll(key, value);
            return true;
        } catch (Exception var3) {
            var3.printStackTrace();
            return false;
        }
    }

    public static boolean lSet(String key, List<Object> value, long time) {
        try {
            REDIS_TEMPLATE.opsForList().rightPushAll(key, value);
            if (time > 0L) {
                expire(key, time);
            }

            return true;
        } catch (Exception var5) {
            var5.printStackTrace();
            return false;
        }
    }

    public static boolean lUpdateIndex(String key, long index, Object value) {
        try {
            REDIS_TEMPLATE.opsForList().set(key, index, value);
            return true;
        } catch (Exception var5) {
            var5.printStackTrace();
            return false;
        }
    }

    public static long lRemove(String key, long count, Object value) {
        try {
            Long remove = REDIS_TEMPLATE.opsForList().remove(key, count, value);
            return remove;
        } catch (Exception var5) {
            var5.printStackTrace();
            return 0L;
        }
    }

    public static boolean zSet(String key, Object value, double score) {
        return Boolean.TRUE.equals(REDIS_TEMPLATE.opsForZSet().add(key, value, score));
    }

    public static Set<Object> zGet(String key, double min, double max) {
        return REDIS_TEMPLATE.opsForZSet().rangeByScore(key, min, max);
    }

    public static long zCount(String key, double min, double max) {
        return (Long) Optional.ofNullable(REDIS_TEMPLATE.opsForZSet().count(key, min, max)).orElse(0L);
    }

    public static boolean zHasKey(String key, Object item) {
        return Objects.nonNull(REDIS_TEMPLATE.opsForZSet().score(key, item));
    }

    public static boolean zRemoveByScore(String key, double min, double max) {
        long removeCount = (Long) Optional.ofNullable(REDIS_TEMPLATE.opsForZSet().removeRangeByScore(key, min, max)).orElse(0L);
        return removeCount > 0L;
    }

    public static boolean tryLock(String lockKey) {
        try {
            return lock(lockKey);
        } catch (InterruptedException var2) {
            var2.printStackTrace();
            return false;
        }
    }

    public static boolean lock(String lockKey) throws InterruptedException {
        int retryCount = 0;

        while (!setNX(lockKey)) {
            if (REDIS_TEMPLATE.getExpire(lockKey, TimeUnit.MILLISECONDS) == -1L) {
                del(lockKey);
            }

            if (5 < retryCount) {
                return false;
            }

            ++retryCount;

            try {
                Thread.sleep(1000L);
            } catch (InterruptedException var3) {
                throw var3;
            }
        }

        return REDIS_TEMPLATE.expire(lockKey, 30000L, TimeUnit.MILLISECONDS);
    }

    public static boolean existLock(String lockKey) {
        return hasKey(lockKey);
    }

    public static void unlock(String key) {
        REDIS_TEMPLATE.execute((paramRedisConnection) -> {
            byte[] lock = key.getBytes(StandardCharsets.UTF_8);
            return paramRedisConnection.del(new byte[][]{lock});
        }, true);
    }

    private static boolean setNX(String lockKey) {
        return (Boolean) REDIS_TEMPLATE.execute((paramRedisConnection) -> {
            byte[] lock = lockKey.getBytes(StandardCharsets.UTF_8);
            return paramRedisConnection.setNX(lock, lock);
        }, true);
    }

    public static <T> T execute(RedisScript<T> script, List<String> keys, Object... args) {
        return REDIS_TEMPLATE.execute(script, keys, args);
    }
}
